﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;

namespace proveKendo.Controllers
{
    public class KendoSchedulerTask : ISchedulerEvent
    {
        [Required]
        public string Description { get; set; }
        [Required]
        public string Title { get; set; }
        public DateTime End { get; set; }
        public string EndTimezone { get; set; }
        public bool IsAllDay { get; set; }
        public int IdMateria { get; set; }
        public int? IdInsegnante { get; set; }
        public int? RoomId { get; set; }
        public List<Room> ListaAule { get; set; }
        public string RecurrenceException { get; set; }
        public int? RecurrenceId { get; set; }
        public string RecurrenceRule { get; set; }
        public DateTime Start { get; set; }
        public string StartTimezone { get; set; }
        public int TaskId { get; set; }
        
    }

    public class ColoriMaterie
    {
        public string Materia { get; set; }
        public int IdMateria { get; set; }
        public string Color { get; set; }
    }


    public class Materia
    {
        public int Id { get; set; }
        public int IdProfessore { get; set; }
        public string Descrizione { get; set; }
    }

    public class Professore
    {
        public int Id { get; set; }
        public string Nome { get; set; }
        public string Cognome { get; set; }
    }

    public class Room
    {
        public int Id { get; set; }
        public string Descrizione { get; set; }
        public string Colore { get; set; }
    }


    public class SchedulerController : Controller
    {
        public ActionResult Index()
        {
           return View();
        }

        public virtual JsonResult Read([DataSourceRequest] DataSourceRequest request)
        {
            DateTime ora = DateTime.Now;

            List<KendoSchedulerTask> listTaskViewModels = new List<KendoSchedulerTask>
            {
                new KendoSchedulerTask
                {
                    Title = "Analisi 1",
                    Description = "Aula A",
                    Start = ora,
                    End = ora.AddHours(1),
                    StartTimezone = "Etc/UTC",
                    EndTimezone = "Etc/UTC",
                    IsAllDay = false,
                    IdMateria = 1,
                    IdInsegnante = 1,
                    RoomId = 1,
                    TaskId = 1
                }
            };

            listTaskViewModels.Add(new KendoSchedulerTask
            {
                Title = "Analisi 1",
                Description = "Aula B",
                Start = ora.AddHours(2),
                End = ora.AddHours(3),
                StartTimezone = "Etc/UTC",
                EndTimezone = "Etc/UTC",
                IsAllDay = false,
                IdMateria = 2,
                IdInsegnante = 2,
                RoomId = 2,
                TaskId = 2
            });

            //listTaskViewModels.Add(new KendoSchedulerTask
            //{
            //    Title = "Reti di calcolatori",
            //    Description = "Aula B",
            //    Start = ora.AddHours(4),
            //    End = ora.AddHours(5),
            //    StartTimezone = "Etc/UTC",
            //    EndTimezone = "Etc/UTC",
            //    IsAllDay = false,
            //    IdMateria = 1,
            //    ColoreMateria = "6397D0",
            //    IdInsegnante = 1,
            //    RoomId = 1,
            //    TaskId = 3
            //});

            //ModelState.AddModelError("errore", "errore");
            //return Json(ModelState.ToDataSourceResult());

            DataSourceResult dataSourceResult = listTaskViewModels.ToDataSourceResult(request);
            return Json(dataSourceResult);
        }


        public virtual JsonResult Create([DataSourceRequest] DataSourceRequest request, KendoSchedulerTask task)
        {
            if (ModelState.IsValid)
            {
                //inserimento del task
                //taskService.Insert(task, ModelState);
            }

            //ModelState.AddModelError("errore", "errore");
            //return Json(ModelState.ToDataSourceResult());

            return Json(new[] { task }.ToDataSourceResult(request, ModelState));
        }


        public virtual JsonResult Destroy([DataSourceRequest] DataSourceRequest request, KendoSchedulerTask task)
        {
            if (ModelState.IsValid)
            {
                //taskService.Delete(task, ModelState);
            }

            ModelState.AddModelError("errore", "errore");
            return Json(ModelState.ToDataSourceResult());

            //var jsonResult = Json(new[] { task }.ToDataSourceResult(request, ModelState));
            //return jsonResult;
        }


        public virtual JsonResult Update([DataSourceRequest] DataSourceRequest request, KendoSchedulerTask task)
        {
            if (ModelState.IsValid)
            {
                
            }

            //ModelState.AddModelError("errore", "errore");
            //return Json(ModelState.ToDataSourceResult());

            return Json(new[] { task }.ToDataSourceResult(request, ModelState));
        }


        public ActionResult GetColorList()
        {
            var colori = new List<ColoriMaterie>
            {
                new ColoriMaterie
                {
                    Materia = "Analisi 1",
                    IdMateria = 1,
                    Color = "#8A2BE2"
                },
                new ColoriMaterie
                {
                    Materia = "Reti di calcolatori",
                    IdMateria = 2,
                    Color = "#E6E6FA"
                }
            };

            return Json(colori, JsonRequestBehavior.AllowGet);
        }

        
    }
}
